$OpenBSD: patch-jruby-launcher_unixlauncher_cpp,v 1.4 2015/07/26 04:40:16 jeremy Exp $

Attempt to replicate the following shell code:

JAVA_HOME=$(javaPathHelper -h jruby)

Without this, if the JAVA_HOME environment variable is not
defined, bin/jruby will segfault when run.

--- jruby-launcher/unixlauncher.cpp.orig	Wed Dec 31 16:00:00 1969
+++ jruby-launcher/unixlauncher.cpp	Mon May 21 14:43:20 2012
@@ -1,5 +1,6 @@
 #include <stdlib.h>
 #include <unistd.h>
+#include <string.h>
 #include "unixlauncher.h"
 #include "utilsfuncs.h"
 
@@ -45,6 +46,44 @@ int UnixLauncher::run(int argc, char* argv[], char* en
     prepareOptions();
 
     string java("");
+
+    if (getenv("JAVA_HOME") == NULL) {
+      char *path;
+      FILE *java_home_pipe;
+      char java_home_path[256];
+
+      path = getenv("PATH");
+      if (path == NULL || strlen(path) == 0) {
+        setenv("PATH", "/usr/bin:${LOCALBASE}/bin:${JAVA_HOME}/bin", 1);
+      } else {
+        char *paths[3] = {(char *)"/usr/bin", (char *)"${LOCALBASE}/bin", (char *)"${JAVA_HOME}/bin"};
+        int modified = 0;
+        int i;
+
+        strlcpy(java_home_path, path, sizeof(java_home_path));
+        for (i = 0; i < 3; i++) {
+          if (strstr(path, paths[i]) == NULL) {
+            modified = 1;
+            strlcat(java_home_path, ":", sizeof(java_home_path));
+            strlcat(java_home_path, paths[i], sizeof(java_home_path));
+          }
+        }
+
+        if (modified == 1) {
+          setenv("PATH", java_home_path, 1);
+        }
+      }
+
+      if(java_home_pipe = popen("javaPathHelper -h jruby", "r")) {
+        size_t bytes_read;
+        bytes_read = fread(java_home_path, 1, 255, java_home_pipe);
+        if (bytes_read > 0) { 
+          java_home_path[bytes_read-1] = '\0';
+          setenv("JAVA_HOME", java_home_path, 1);
+        }
+        pclose(java_home_pipe);
+      }
+    }
 
     if (getenv("JAVACMD") != NULL) {
         java = getenv("JAVACMD");
